﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;

namespace RuEn
{
    /// <summary>
    /// Core Rule Validation Engine
    /// </summary>
    public abstract class CoreEngine
    {
        #region Private Variables
        private BrokenRules _brokenRules = new BrokenRules();
        #endregion


        #region Private Methods - Validation Method
        /// <summary>
        /// Private Method which contains the main logic for validation
        /// </summary>
        /// <param name="instance">object instance</param>
        private void ValidateRules(object instance)
        {
            Type type = instance.GetType();
            object value;
            object[] objects;
            PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            if (props.Length > 0)
            {
                foreach (PropertyInfo prop in props)
                {
                    value = prop.GetValue(instance, null);
                    objects = prop.GetCustomAttributes(typeof(RuleBase), true);

                    foreach (Attribute atts in objects)
                    {
                        if (atts is RequiredStringRule)
                        {
                            RequiredStringRule reqStrRule = (RequiredStringRule)atts;
                            if (!reqStrRule.Validate(Convert.ToString(value), reqStrRule.ErrorMessage))
                            {
                                _brokenRules.Add(reqStrRule.ErrorMessage);
                            }
                        }
                        else if (atts is RequiredObjectRule)
                        {
                            RequiredObjectRule reqObjRule = (RequiredObjectRule)atts;
                            if (!reqObjRule.Validate(value, reqObjRule.ErrorMessage))
                            {
                                _brokenRules.Add(reqObjRule.ErrorMessage);
                            }
                        }
                        else if (atts is RegExRule)
                        {
                            RegExRule regExRule = (RegExRule)atts;
                            if (!regExRule.Validate(Convert.ToString(value), regExRule.Expression, regExRule.ErrorMessage))
                            {
                                _brokenRules.Add(regExRule.ErrorMessage);
                            }
                        }
                        else if (atts is LengthRule)
                        {
                            LengthRule lenRule = (LengthRule)atts;
                            if (!lenRule.Validate(Convert.ToString(value), lenRule.MaxLength, lenRule.MinLength, lenRule.ErrorMessage))
                            {
                                _brokenRules.Add(lenRule.ErrorMessage);
                            }
                        }
                        else if (atts is ComparerBoolRule)
                        {
                            ComparerBoolRule comBoolRule = (ComparerBoolRule)atts;
                            if (!comBoolRule.Validate(Convert.ToBoolean(value), comBoolRule.Comparer,
                                comBoolRule.ErrorMessage, comBoolRule.Operation))
                            {
                                _brokenRules.Add(comBoolRule.ErrorMessage);
                            }
                        }
                        else if (atts is ComparerIntRule)
                        {
                            ComparerIntRule compIntRule = (ComparerIntRule)atts;
                            if (!compIntRule.Validate(Convert.ToInt32(value), compIntRule.Comparer,
                                compIntRule.ErrorMessage, compIntRule.Operation))
                            {
                                _brokenRules.Add(compIntRule.ErrorMessage);
                            }
                        }
                        else if (atts is ComparerObjectRule)
                        {
                            ComparerObjectRule compObjRule = (ComparerObjectRule)atts;
                            if (!compObjRule.Validate(value, compObjRule.Comparer,
                                compObjRule.ErrorMessage, compObjRule.Operation))
                            {
                                _brokenRules.Add(compObjRule.ErrorMessage);
                            }
                        }
                        else if (atts is ComparerStringRule)
                        {
                            ComparerStringRule compStrRule = (ComparerStringRule)atts;
                            if (!compStrRule.Validate(Convert.ToString(value), compStrRule.Comparer,
                                compStrRule.ErrorMessage, compStrRule.Operation))
                            {
                                _brokenRules.Add(compStrRule.ErrorMessage);
                            }
                        }
                        else if (atts is IntCollectionRule)
                        {
                            IntCollectionRule intCollRule = (IntCollectionRule)atts;
                            if (!intCollRule.Validate((IEnumerable<int>)value, intCollRule.ErrorMessage))
                            {
                                _brokenRules.Add(intCollRule.ErrorMessage);
                            }
                        }
                        else if (atts is ObjectCollectionRule)
                        {
                            ObjectCollectionRule intObjRule = (ObjectCollectionRule)atts;
                            if (!intObjRule.Validate((IList)value, intObjRule.ErrorMessage))
                            {
                                _brokenRules.Add(intObjRule.ErrorMessage);
                            }
                        }
                        else if (atts is StringCollectionRule)
                        {
                            StringCollectionRule strCollRule = (StringCollectionRule)atts;
                            if (!strCollRule.Validate((IEnumerable<string>)value, strCollRule.ErrorMessage))
                            {
                                _brokenRules.Add(strCollRule.ErrorMessage);
                            }
                        }
                    }
                }
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Public method which calls the private method containg the logic for rules valudation
        /// </summary>
        /// <param name="instance">object instance (e.g obj.Validate(obj))</param>
        public virtual void Validate(object instance)
        {
            this.ValidateRules(instance);
        }

        /// <summary>
        /// Public property to flag if the concerned object is valid, i.e. free of broken rules
        /// </summary>
        public bool isValid
        {
            get 
            {
                if (_brokenRules == null || _brokenRules.Count == 0)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Provides an collection of broken rules in the concerned object
        /// </summary>
        public BrokenRules BrokenRules
        {
            get { return _brokenRules; }
        }
        #endregion
    }
}
